package com.xying.common;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: Hisen
 * @date: 2018/4/3 15:58
 * @description:
 */
@Slf4j
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
  @Override
  public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    log.error("{} Exception", httpServletRequest.getRequestURI(), e);

    ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
    modelAndView.addObject("status", ResponseCode.ERROR.getCode());
    modelAndView.addObject("msg", ResponseCode.SERVER_ERROR.getCode());
    modelAndView.addObject("data", e.toString());
    return modelAndView;
  }
}
